package com.zhou.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhou.constant.MessageConstant;
import com.zhou.dao.MenuMapper;
import com.zhou.entity.PageResult;
import com.zhou.entity.QueryPageBean;
import com.zhou.entity.Result;
import com.zhou.pojo.Menu;
import com.zhou.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    /**
     * 根据用户名查询菜单
     *
     * @param username
     * @return
     */
    @Override
    public List<Menu> findMenuByUsername(String username) {
        return menuMapper.findMenuByUsername(username);
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Menu> page = menuMapper.getTreeData(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 查询父菜单信息
     *
     * @return
     */
    @Override
    public List<Menu> findAllParent() {
        return menuMapper.findAllParent();
    }

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @Override
    public Result add(Menu menu) {
        //先判断菜单是否存在
        long count = menuMapper.findExist(menu);
        if (count > 0) {
            return new Result(false, "该权限菜单已经存在,无法创建");
        }
        menuMapper.insert(menu);
        return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }

    /**
     * 根据id查询菜单
     *
     * @param id
     * @return
     */
    @Override
    public Menu findById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    /**
     * 编辑菜单
     *
     * @param menu
     */
    @Override
    public void edit(Menu menu) {
        menuMapper.updateByPrimaryKeySelective(menu);
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        //判断当前菜单是否存在子菜单
        long count = menuMapper.findCountByCheckMenuId(id);
        if (count > 0) {
            //存在子菜单，不允许删除
            return new Result(false, "要删除的菜单存在子菜单,无法删除！");
        }
        //删除 t_role_menu 表中的菜单数据
        menuMapper.deleteRoleMenuById(id);
        //删除 t_menu 表
        menuMapper.deleteByPrimaryKey(id);
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }

    /**
     * 查询所有菜单
     *
     * @return
     */
    @Override
    public List<Menu> findAll() {
        return menuMapper.findAll();
    }

    /**
     * 根据角色id查询菜单id
     *
     * @param id
     * @return
     */
    @Override
    public Integer[] findMenuIdsByRoleId(Integer id) {
        return menuMapper.findMenuIdsByRoleId(id);
    }
}
